from fastapi import APIRouter, Depends, HTTPException
from typing import List
from basic import BaseSchema
from service import FileService, IFileService

router = APIRouter(prefix="/files", tags=["files"])

class FileCreateRequest(BaseSchema):
    repo_key: str
    file_name: str
    object_key: str

class FileCreateResponse(BaseSchema):
    file_key: str

@router.post("/create", response_model=FileCreateResponse)
async def create_file(
    params: FileCreateRequest, 
    service: IFileService = Depends(FileService)
):
    try:
        return await service.create_file(params.repo_key, params.file_name, params.object_key)
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))


class FileUpdateRequest(BaseSchema):
    repo_key: str
    file_key: str
    update_key: str
    update_value: str

class FileUpdateResponse(BaseSchema):
    file_key: str

@router.post("/update", response_model=FileUpdateResponse)
async def update_file(
    params: FileUpdateRequest, 
    service: IFileService = Depends(FileService)
):
    try:
        return await service.update_file(params.repo_key, params.file_key, params.update_key, params.update_value)
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

class FileMetadataRequest(BaseSchema):
    repo_key: str
    file_key: str

class FileMetadataResponse(BaseSchema):
    file_key: str
    file_name: str
    download_url: str

@router.post("/metadata", response_model=FileMetadataResponse)
async def get_file_info(
    params: FileMetadataRequest, 
    service: IFileService = Depends(FileService)
):
    try:
        return await service.metadata(params.repo_key, params.file_key)
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

